﻿using Hotel.BLL;
using Hotel.Model.Member;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MD5Hash;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;

namespace Hotel.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemberController : ControllerBase
    {
        MemberBLL bll;
        public MemberController(MemberBLL bll)
        {
            this.bll = bll;
        }

        /// <summary>
        /// 完成会员注册
        /// </summary>
        /// <param name="info">注册的实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public IActionResult PostMemberRegiter(T_Member info)
        {
            try
            {
               //info.M_Id = Guid.NewGuid(); //表示每次都会生成一个新的GUid
               int res = bll.PostMemberRegiter(info);
                return Ok(new
                {
                    data = res, // 1 -1 -2
                });

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="pwd">密码</param>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult GetMemeberLogin(string phone, string pwd)
        {
            try
            {
                //对密码进行加密
                pwd = pwd.GetMD5();
                T_Member info = bll.GetMemeberLogin(phone, pwd);
                return Ok(new
                {
                    data = info
                });
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="phone">根据手机号查询</param>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult GetMembers(string? phone = "")
        {
            try
            {
                List<T_Member> list = bll.GetMembers(phone);
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDelMemberItem(int mid)
        {
            try
            {
                int res = bll.GetDelMemberItem(mid);
                return Ok(new
                {
                    data = res
                });
                
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 根据id修改状态
        /// </summary>
        /// <param name="mid">id</param>
        /// <param name="state">转台</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUpdateMemberItem(int mid, int state)
        {
            try
            {
                var res = bll.GetUpdateMemberItem(mid, state);
                return Ok(new
                {
                    data = res
                });


            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExportExcel()
        {
            try
            {
                //声明一个空的字节数组
                byte[] data = null;
                //指定导出的文件类型
                string contentType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                //指定文件的名字
                string fileName = "会员列表.xlsx";


                //新的Excel工作簿
                //创建Excel工作簿
                IWorkbook book = new XSSFWorkbook();
                //创建一个工作表
                ISheet sheet = book.CreateSheet();
                //创建第一行
                IRow row1 = sheet.CreateRow(0);

                //给第一行指定标题
                row1.CreateCell(0).SetCellValue("昵称");
                row1.CreateCell(1).SetCellValue("手机号");
                row1.CreateCell(2).SetCellValue("身份证号码");

                //获取数据库中数据
                var list = bll.GetMembers();
                //表示第一行
                int rowIndex = 1;
                //循环数据
                foreach (var item in list)
                {
                    IRow row = sheet.CreateRow(rowIndex);
                    row.CreateCell(0).SetCellValue(item.M_Name);
                    row.CreateCell(1).SetCellValue(item.M_Phone);
                    row.CreateCell(2).SetCellValue(item.M_Card);

                    rowIndex++;
                }
                //为系统内存提供流式的读写操作
                MemoryStream ms = new MemoryStream();
                //将文件内容写入到内存
                book.Write(ms);
                //内存流 赋给 数据字节数组中
                data = ms.ToArray();

                //注释： File是控制器自带方法，不是IO流中的File  // 返回一个文件结果
                return File(data, contentType, fileName);

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 导入excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ImportExcel(IFormFile file)
        {
            try
            {
                // 读出上传的Excel内容到内存流中
                MemoryStream ms = new MemoryStream();
                file.CopyTo(ms);
                // 文件内容跳转到开始位置
                ms.Position = 0;

                //创建XSSF工作簿 并且将内存流中的内容放到工作表中
                IWorkbook wb = new XSSFWorkbook(ms);
                //获取第一个sheet
                ISheet sheet = wb.GetSheetAt(0);
                // 总行数
                int totalRows = sheet.LastRowNum;

                //创建会员表集合
                List<T_Member> memberList = new List<T_Member>();

                //循环总行数
                for (int i = 1; i <= totalRows; i++)
                {
                    IRow row = sheet.GetRow(i); // 获取行

                    string M_Name = row.GetCell(0).ToString();
                    string M_Phone = row.GetCell(1).ToString();
                    string M_Card = row.GetCell(2).ToString();

                    //给会员进行赋值
                    T_Member Member = new T_Member()
                    {
                        M_Name = M_Name, 
                        M_Phone = M_Phone,
                        M_Card = M_Card
                    };
                    //添加每一行数据
                    memberList.Add(Member);
                }
                //关闭
                wb.Close();

                //调接口添加到数据库中

                int res = bll.PostAddMemberList(memberList);

                return Ok(new
                {
                    data = res
                });

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据会员的ID获取会员信息
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult GetMember(int Mid)
        {

            try
            {
                T_Member info = bll.GetMember(Mid);
                return Ok(new
                {
                    data = info
                });
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 对会员进行修改
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回影响的行数</returns>
        [HttpPost]
        public IActionResult PostUpdateMember(T_Member info)
        {
            try
            {
                int res = bll.PostUpdateMember(info);
                return Ok(new
                {
                    data = res
                });

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加会员等级
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddMemberLevel(T_Member_Level info)
        {
            try
            {
                int res = bll.PostAddMemberLevel(info);
                return Ok(new
                {
                    data = res
                });
              
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 等级列表
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="LevelName">等级名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMemberLevelList(string? LevelName = "", int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //所有的等级列表
                int totalPage = 0;
                int totalCount = 0;
                List<T_Member_Level> list = bll.GetMemberLevelList(out totalPage, out totalCount, LevelName, pageIndex, pageSize);

                return Ok(new
                {
                    data = list,
                    totalPage = totalPage,
                    totalCount = totalCount
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加会员权益
        /// </summary>
        /// <param name="info">会员权益</param>
        /// <returns>返回影响的行数</returns>
        [HttpPost]
        public IActionResult PostAddMemberRight(T_Member_Right info)
        {
            try
            {
                //1.判断一下权益名称是重复
                int r = bll.PostAddMemberRight(info);
               return Ok(new
               {
                   data = r
               });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMemberRights()
        {
            try
            {
                List<T_Member_Right> list = bll.GetMemberRights();
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加等级权益
        /// </summary>
        /// <param name="rightArr">权益id</param>
        /// <param name="levelId">等级id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult PostAddLevelRight(string rightArr, int levelId)
        {
            try
            {

                int res = bll.PostAddLevelRight(rightArr, levelId);
                return Ok(new
                {
                    data = res
                });
                
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据等级id去查他的权益
        /// </summary>
        /// <param name="levevlId">等级id</param>
        /// <returns>返回符合条件的数据</returns>
        [HttpGet]
        public IActionResult GetLevelRightList(int levevlId)
        {
            try
            {
                List<T_Level_Right> list = bll.GetLevelRightList(levevlId);
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
